//
//  SDHSportsTeamInfoVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/8.
//

#import "SDHSportsTeamInfoVC.h"
#import "SDHSportsTeamInfoDetailVC.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "SDHSportsTeamInfoModel.h"
#import "MJExtension.h"
#import "UIImageView+YQAdd.h"
#import "SDHSportsMatchSoccerModel.h"
#import "SDHSportsDataManager.h"

const static CGFloat kHeaderHeight = 150;

@interface SDHSportsTeamInfoVC ()

@property (nonatomic, strong) UIView *teamInfoHeaderView;
@property (nonatomic, strong) UIImageView *avatarImg;
@property (nonatomic, strong) UILabel *nameLb;
@property (nonatomic, strong) UILabel *teamInfoLb;

@property (nonatomic, strong) UIButton *btnFollow;

@property (nonatomic, strong) SDHSportsTeamInfoModel *infoModel;

@end

@implementation SDHSportsTeamInfoVC

- (id)init {
    if (self = [super init]) {
        
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.title = @"Team Information";

    [self initUI];
    [self loadData];
}

- (void)initUI {
    UIView *hView = [UIView new];
    [self.view addSubview:hView];
    hView.backgroundColor = [UIColor blackColor];
    
    __weak typeof(self) weakSelf = self;
    CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
    CGFloat navigationHeight = self.navigationController.navigationBar.size.height;
    [hView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsMake(200, 0, 0, 0)).priorityLow();
        make.top.equalTo(weakSelf.view).offset(statusBarHeight+navigationHeight+10);
        make.height.equalTo(@(kHeaderHeight));
    }];
    
    UIImageView *imgView = [UIImageView new];
    imgView.image = [UIImage imageNamed:@"touxiang"];
    [hView addSubview:imgView];
    self.avatarImg = imgView;
    
    UILabel *nameLb = [UILabel new];
    nameLb.textAlignment = NSTextAlignmentLeft;
    nameLb.textColor = [UIColor whiteColor];
    nameLb.numberOfLines = 0;
    nameLb.font = [UIFont systemFontOfSize:16];
    [hView addSubview:nameLb];
    nameLb.text = @"Manchester United";
    self.nameLb = nameLb;
    
    UIButton *followBtn = [UIButton new];
    followBtn.titleLabel.text = @"Follow";
    [followBtn setTitle:@"Follow" forState:UIControlStateNormal];
    followBtn.clipsToBounds = YES;
    followBtn.layer.cornerRadius = 15;
    [followBtn setBackgroundColor:UIColorFromRGB(255, 129, 67)];
    UIImage *normalColor = [UIImage imageFromColor:UIColorFromRGB(255, 129, 67) size:CGSizeMake(80, 30)];
    UIImage *highlightColor = [UIImage imageFromColor:[UIColor grayColor] size:CGSizeMake(80, 30)];
    [followBtn setBackgroundImage:normalColor forState:UIControlStateNormal];
    [followBtn setBackgroundImage:highlightColor forState:UIControlStateHighlighted];
    [followBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [followBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [hView addSubview:followBtn];
    self.btnFollow = followBtn;
    followBtn.userInteractionEnabled = NO;
    [followBtn addTarget:self action:@selector(follow:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *teamInfoBg = [UIView new];
    teamInfoBg.clipsToBounds = YES;
    teamInfoBg.layer.cornerRadius = 8;
    teamInfoBg.backgroundColor = UIColorFromRGB(72, 72, 72);
    [hView addSubview:teamInfoBg];
    
    UILabel *teamInfoLabel = [UILabel new];
    teamInfoLabel.textAlignment = NSTextAlignmentLeft;
    teamInfoLabel.numberOfLines = 0;
    teamInfoLabel.textColor = UIColorFromRGB(159, 159, 159);
    teamInfoLabel.font = [UIFont systemFontOfSize:14];
    teamInfoLabel.backgroundColor = [UIColor clearColor];
    [teamInfoBg addSubview:teamInfoLabel];
    teamInfoLabel.text = @"City: Manchester  Estabilished time: 1878\nStadium: Old Trafford Stadium\nHead Coach: Ol Gunnar Solskjaer";
    self.teamInfoLb = teamInfoLabel;
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hView).offset(10);
        make.top.equalTo(hView).offset(10);
        make.width.equalTo(@50);
        make.height.equalTo(@50);
    }];
    
    [nameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.mas_right).offset(10);
        make.centerY.equalTo(imgView);
    }];
    
    [followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(hView).offset(-20);
        make.centerY.equalTo(imgView);
        make.width.equalTo(@80);
        make.height.equalTo(@30);
    }];
    
    [teamInfoBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgView.mas_bottom).offset(10);
        make.left.equalTo(hView).offset(10);
        make.right.equalTo(hView).offset(-10);
        make.bottom.equalTo(teamInfoLabel.mas_bottom).offset(10);
    }];
    
    [teamInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(teamInfoBg).insets(UIEdgeInsetsMake(10, 10, 10, 5));
    }];
    
    UIView *detailView = [UIView new];
    [self.view addSubview:detailView];
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(hView.mas_bottom).offset(0);
        make.left.equalTo(hView);
        make.right.equalTo(hView);
        make.bottom.equalTo(weakSelf.view.mas_bottom);
    }];
    
    SDHSportsTeamInfoDetailVC *vc = [[SDHSportsTeamInfoDetailVC alloc] init];
    vc.teamInfoDic = _teamInfoDic;
    [detailView addSubview:vc.view];
    [self addChildViewController:vc];
    [vc.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(detailView).offset(0);
        make.left.equalTo(detailView).offset(0);
        make.right.equalTo(detailView).offset(0);
        make.bottom.equalTo(detailView).offset(0);
    }];
}

- (void)follow:(id)sender {
    [MBProgressHUD showSuccess:@"Follow succeed!"];
    [[SDHSportsDataManager sharedManager] addFollowTeam:self.infoModel];
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    
    self.btnFollow.userInteractionEnabled = YES;
    [[SDHSportsDataManager sharedManager] addBrowseTeam:self.infoModel];
    
    [self.avatarImg setImageWithURL:[NSURL URLWithString:_infoModel.logo] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    self.nameLb.text = _infoModel.en_name;
    NSString *city = _infoModel.city ? _infoModel.city : @"";
    NSString *time = _infoModel.established_date ? _infoModel.established_date : @"";
    NSString *stadium = _infoModel.stadium ? _infoModel.stadium : @"";
    NSString *coach = _infoModel.coach_name ? _infoModel.coach_name : @"";
    self.teamInfoLb.text = [NSString stringWithFormat:@"City: %@  Estabilished time: %@\nStadium: %@\nHead Coach: %@\n", city, time, stadium, coach];
}

- (void)loadData {
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyu1/getTeamDetail";
    NSString *teamId = _teamInfoDic[@"team_id"];
    if (teamId.length == 0) {
        teamId = @"100029";
    }
    request.requestPara = @{@"id":teamId};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        weakSelf.infoModel = [SDHSportsTeamInfoModel mj_objectWithKeyValues:obj[@"data"]];
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}


@end
